/*
 * @Author: qjb
 *  系统日志
 * @Date: 2018-03-13 14:44:57 
 */
layui.config({}).use(['element', 'zlConfig', 'zltreemenu', 'zlContext', 'table', 'layer'], function (exports) {
	var $ = layui.$,
		zlContext = layui.zlContext,
		zltreemenu = layui.zltreemenu,
		layer = layui.layer,
		table = layui.table,
		form = layui.form,
		zlConfig = layui.zlConfig,
		fullheight = 'full-30',
		customCurr = 1;

	var treeObj,
		isloadsearch = false,
		treeAction = {
			loadTree: function () {
				//创建zTree组件
				treeObj = zltreemenu.render('orgTreeId', {
					dataurl: layui.cache['contentPath'] + '/admin/coreorg/queryorgtreelistbyparentid',
					autoParam: ["RWID"],
					tableId: 'tbUser',
					isMove: false
				}, {
					onClick: function (event, treeId, treeNode) {
						customCurr = 1;
						//触发刷新的事件
						treeAction.raiseResDataEvent(treeNode);
					},
					onAsyncSuccess: function (event, treeId, treeNode, msg) {
						treeObj.selectNode(treeObj.getNodes()[0]);
						treeAction.raiseResDataEvent(treeObj.getNodes()[0]);
					}
				})
			},
			/**发起刷新系统日志事件 */
			raiseResDataEvent: function (node) {
				var currNode = node;
				if (!node) {
					var nodes = treeObj.getSelectedNodes();
					if (nodes.length > 0)
						currNode = nodes[0];
				}
				layui.event('WM_USERDEF', zlConfig.global.WM_USERDEF.REFRESHDATA_DATA, currNode);
			},
		}
	//加载树
	treeAction.loadTree();

	//渲染用户列表
	var userTable = table.render({
		elem: '#tbUser',
		height: fullheight,
		response: zlConfig.global.gridResponse,
		limit: 20,
		limits: [20, 50, 100, 200, 500, 1000],
		page: true,
		sortload: true,
		updId: "4A2ED2A03F4848F2B7DD0AFFD1E7A5C1",
		toolbar: '#toolbarSyslog',
		defaultToolbar: ['filter'],
		cols: [
			[ //表头
				{
					type: 'numbers',
					fixed: 'left',
					title: '#'
				}, {
					field: 'LEIXNAME',
					title: '级别',
					width: 75
				}, {
					field: 'SYS_CREATETIME',
					title: '操作时间',
					width: 200,
					sort: true
				}, {
					field: 'CAOZIP',
					title: '操作IP地址',
					width: 120,
				}, {
					field: 'YONGHMC',
					title: '操作人',
					width: 100,
				}, {
					field: 'MIAOS',
					title: '操作内容',
					width: 450
				}, {
					field: 'SUOSMKID',
					title: '操作模块',
					width: 100,
					sort: true
				}
			]
		]
	});

	var gridAction = {
		currentEditObj: undefined,
		search: function () {
			var nodes = treeObj.getSelectedNodes();
			var node = null;
			if (nodes.length > 0) {
				node = nodes[0];
			}
			customCurr = 1;
			layui.event('WM_USERDEF', zlConfig.global.WM_USERDEF.REFRESHDATA_DATA, node);
		},
	}

	$('#btnUserNew').on('click', function () {
		gridAction.newRow();
	});
	$('#btnUserDelete').on('click', function () {
		var checkStatus = table.checkStatus('tbUser'),
			rows = checkStatus.data;
		gridAction.deleteRows(rows);
	});
	// $('.zl-container').on('click','#btnSeacher', function () {
	// 	gridAction.search();
	// });
	$('.zl-container').on('keyup','#edtSeacherValue', function (event) {
		var keyCode = event.keyCode;
		customCurr = 1;
		$('.zl-container').attr('data-search',$(this).val())
		if (keyCode == 13) {
			gridAction.search();
		}
		if ((keyCode == '46' || keyCode == '8' || (event.ctrlKey && keyCode == '86')) && $(this).val() == '' && isloadsearch) {
			$('#btnSeacher').trigger('click');
			isloadsearch = false;
		}
	});
	//头工具栏事件
	table.on('toolbar(tbUser)', function(obj){
		switch(obj.event){
			case 'btnSeacher':
				customCurr = 1;
				gridAction.search();
				break;
		};
	});
	
	table.on('tool(tbUser)', function (obj) {
		var row = obj.data; //获得当前行数据
		var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
		if (layEvent === 'detail') { //查看
			gridAction.viewRow(row);
		} else if (layEvent === 'del') { //删除
			gridAction.currentEditObj = obj;
			gridAction.deleteRows([obj]);
		} else if (layEvent === 'edit') { //编辑
			gridAction.currentEditObj = obj;
			gridAction.editRow(row);
		}
	});
	//提交
	form.on('submit(frmGridUser)', function (frm) {
		return gridAction.sumbit(frm.field);
	});

	//监听刷新的事件
	layui.event('WM_USERDEF', zlConfig.global.WM_USERDEF.REFRESHDATA_DATA, null, function (node) {
		var index = layer.msg('数据查询中，请稍候...', {
			icon: 16,
			time: false,
			shade: 0.1
		});
		var loadGridData = function (id, url, param) {
			userTable.reload({
				url: url,
				method: 'post',
				page: {
					curr: customCurr
				},
				where: param,
				done: function (res, curr, count) {
					$('#edtSeacherValue').val($('.zl-container').attr('data-search'))
					layer.close(index);
					customCurr = curr
				}
			}, true);
		}
		var params = null;
		var keyvallue = $('#edtSeacherValue').val();
		if (node != null && keyvallue != '') {
			params = {
				ORGID: node.RWID,
				KEYVALUE: '%' + keyvallue + '%'
			};
			isloadsearch = true;
		} else if (node == null && keyvallue != '') {
			params = {
				KEYVALUE: '%' + keyvallue + '%'
			};
			isloadsearch = true;
		} else if (node != null && keyvallue == '') {
			params = {
				ORGID: node.RWID
			};
		}
		
		loadGridData('tbUser', layui.cache['contentPath'] + '/admin/coresyslog/query', params);
	});
	
	//iframe自适应
	$(window).on('resize', function () {
		zlContext.setMaxHeihgt(0);
	}).resize();

});